package com.bbms.controller.bookManage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbms.entity.Book;
import com.bbms.entity.ErrorCode;
import com.bbms.entity.Page;
import com.bbms.entity.Result;
import com.bbms.entity.SuccessCode;
import com.bbms.service.BookService;

/**
 * @author Administrator
 *	图书管理控制器
 */
@Controller
@RequestMapping("/bbms/bookManage")
public class BookManageController {
	
	// 字符串转日期绑定
	@InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
	
	@Resource(name="BookServiceImpl")
	private BookService bService;
	
	@RequestMapping("/bookManage")
	public String bookManage(){
		return "bookManage/bookManage.jsp";
	}
	
	/**
	 * 图书分页信息
	 * @param page 当前页
	 * @param rows 行数
	 * @param s_name 名字
	 * @param s_author 作者
	 * @param s_publisherId 出版社id
	 * @param s_categoryId 类型id
	 * @param start_time 开始时间
	 * @param end_time 结束时间
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public Page getPage(int page, int rows, String s_name, String s_author, Integer s_publisherId,
							Integer s_categoryId, String start_time, String end_time, Integer authority,
							HttpServletRequest request){
		
		System.out.println( s_name + "  " + s_author+ "  " +  s_publisherId+ "  " + s_categoryId+ "  " +  start_time+ "  " + end_time);
		
		int auth = authority != null ? authority : (int) request.getSession().getAttribute("authority");
		
		Page pageData = bService.getPage(page, rows, s_name, s_author, s_publisherId, s_categoryId, start_time, end_time, auth);
		
		return pageData;
	}
	
	/**
	 * 保存图书
	 * @param book 图书信息
	 * @param booksNum 数量
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public Result insert(@ModelAttribute("book") Book book, int booksNum){
		Result r = new Result();
		
		if (book.getId() == 0){
			try{
				
				bService.addBatch(book, booksNum);
				r.setStatus(SuccessCode.insertSuccess.getCode());
				r.setMsg(SuccessCode.insertSuccess.getName());
				
			} catch (Exception e) {
				e.printStackTrace();
				r.setStatus(ErrorCode.insertFailed.getCode());
				r.setMsg(ErrorCode.insertFailed.getName());
			}
		} else {
			try{
				
				bService.updateBatch(book);
				r.setStatus(SuccessCode.updateSuccess.getCode());
				r.setMsg(SuccessCode.updateSuccess.getName());
				
			} catch (Exception e) {
				e.printStackTrace();
				r.setStatus(ErrorCode.updateFailed.getCode());
				r.setMsg(ErrorCode.updateFailed.getName());
			}
		}
		
		return r;
	}
	
	/**
	 * 删除出版社
	 * @param publisher 出版社对象
	 * @return 结果信息
	 */
	@ResponseBody
	@RequestMapping(value="delBatch", method=RequestMethod.POST)
	public Result delBatch(@ModelAttribute(name="book") Book book){
		Result r = new Result();
		try{
			bService.delBatch(book);
			r.setStatus(SuccessCode.deleteSuccess.getCode());
			r.setMsg(SuccessCode.deleteSuccess.getName());
		} catch (Exception e) {
			e.printStackTrace();
			r.setStatus(ErrorCode.deleteFailed.getCode());
			r.setMsg(ErrorCode.deleteFailed.getName());
		}
		
		return r;
	}
	
	/**
	 * 获得图书详细
	 * @param publishDate 出版日期
	 * @param ISBN 书码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="detailList", method=RequestMethod.POST)
	public List<?> detailList(String publishDate, String ISBN, Integer authority, HttpServletRequest request){
		
		int auth = authority != null ? authority : (int) request.getSession().getAttribute("authority");
		List<?> list = bService.getDetail(publishDate, ISBN, auth);
		
		return list;
	}
	
	/**
	 * 修改图书详细信息
	 * @param book 实体
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value="updateDetail", method=RequestMethod.POST)
	public Result updateDetail(@ModelAttribute(name="book") Book book){
		Result r = new Result();
		try{
			bService.update(book);
			r.setStatus(SuccessCode.updateSuccess.getCode());
			r.setMsg(SuccessCode.updateSuccess.getName());
		} catch (Exception e) {
			e.printStackTrace();
			r.setStatus(ErrorCode.updateFailed.getCode());
			r.setMsg(ErrorCode.updateFailed.getName());
		}
		
		return r;
	}
	
	/**
	 * 删除图书信息
	 * @param book 实体
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delDetail", method=RequestMethod.POST)
	public Result delDetail(@ModelAttribute(name="book") Book book){
		Result r = new Result();
		try{
			bService.delete(book);
			r.setStatus(SuccessCode.deleteSuccess.getCode());
			r.setMsg(SuccessCode.deleteSuccess.getName());
		} catch (Exception e) {
			e.printStackTrace();
			r.setStatus(ErrorCode.deleteFailed.getCode());
			r.setMsg(ErrorCode.deleteFailed.getName());
		}
		
		return r;
	}
}
